package com.hclc.config;


import com.hclc.api.redis.IRedisService;
import com.hclc.config.util.TokenUtil;
import com.hclc.entity.model.enums.PortTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.servlet.AdviceFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * @Author: hef
 * @Date: Create in 3:51 下午 2020/8/22
 */
@Slf4j
@Component
public class AddResponseHeaderFilter extends AdviceFilter {

    @Autowired
    private IRedisService redisService;

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        res.setContentType("application/json; charset=utf-8");
        res.setHeader("Access-Control-Allow-Credentials", "true");
        res.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
        res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, token");
        res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
        res.setHeader("Cache-Control", "no-cache");
        String token = Optional.ofNullable(req.getHeader("token")).orElse("");

        try {
            // app
            if (!TokenUtil.isSpecifyPortType(token, PortTypeEnum.BACKSTAGE)) {
                // 得到用户信息并反序列化
//                String userInfo = redisService.get(token);
//                UserRedisDto<UserCustomerModel> userRedisDto = JsonUtil.toObject(userInfo, UserRedisDto.class);
//                if (Objects.nonNull(userRedisDto)) {
//                    redisService.set(token, userInfo, com.hclc.hxwy.model.contans.ApiConst.USER_TOKEN_TIME_OUT_DAY, TimeUnit.DAYS);
//                } else {
//                    UserCustomerModel user = new UserCustomerModel();
//                    user.setUserId(ApiConst.ANONYMOUS_USER_ID);
//                    user.setUserName("匿名用户");
//                    user.setUserAccount("匿名用户");
//                    user.setUserPhone("");
//                    user.setUserLogo("");
//                    user.setUserMotto("");
//                    user.setUserGender(UserGenderEnum.MALE);
//                    user.setUserLabel("");
//                    user.setBirth(null);
//                    user.setFansNum(0);
//                    user.setAttentionNum(0);
//                    user.setFavoritesNum(0);
//                    user.setNewsNum(0);
//                    user.setRegionFullName("");
//                    user.setIsRealName(IsRealNameEnum.NO);
//                    user.setEnable(ApiConst.ENABLE_TRUE);
//
//                    userRedisDto = new UserRedisDto();
//                    userRedisDto.setData(user);
//                    userRedisDto.setToken("");
//                }
//                userRedisDto.setData(JsonUtil.toObject(JsonUtil.toJson(userRedisDto.getData()), UserCustomerModel.class));
//                // 初始化系统环境上下文
//                AppContext.initSystemEnvironmentContext(req, res, false);
//                // 初始化当前登录用户信息
//                AppContext.initCurrentUser(UserCustomerModel.class.getName(), userRedisDto.getData(), userRedisDto.getToken(), userRedisDto.getData().getUserId());

            }
        } catch (Exception e) {
            log.error("", e);
        }
        return true;
    }
}
